package com.demo.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.demo.blog.mapper.BlogInfoMapper;
import com.demo.blog.pojo.dataobject.BlogInfo;
import com.demo.blog.pojo.response.BlogDetailResponse;
import com.demo.blog.pojo.response.BlogInfoResponse;
import com.demo.blog.service.BlogService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogInfoMapper blogInfoMapper;

    @Override
    public List<BlogInfoResponse> getList() {
        //从数据库中查询所以字段
        //mybatis-plus中查询
        LambdaQueryWrapper<BlogInfo> queryWrapper = new LambdaQueryWrapper<>();
        List<BlogInfo> blogInfos = blogInfoMapper.selectList(queryWrapper
                .eq(BlogInfo::getDeleteFlag, 0)
                .orderByAsc(BlogInfo::getId));
        //转换为响应对象
        List<BlogInfoResponse> blogInfoResponses = blogInfos.stream().map(blogInfo -> {
            BlogInfoResponse blogInfoResponse = new BlogInfoResponse();
            BeanUtils.copyProperties(blogInfo, blogInfoResponse);
            return blogInfoResponse;
        }).collect(Collectors.toList());

        return blogInfoResponses;
    }

    @Override
    public BlogDetailResponse getBlogDeatail(Integer blogId) {
        BlogDetailResponse blogDetailResponse = new BlogDetailResponse();
        //从数据库中查询所有字段,根据ID查询
//        BlogInfo blogInfo = blogInfoMapper.selectOne(new LambdaQueryWrapper<BlogInfo>()
//                .eq(BlogInfo::getId, blogId)
//                .eq(BlogInfo::getDeleteFlag, 0));
        BlogInfo blogInfo = blogInfoMapper.selectById(blogId);
        BeanUtils.copyProperties(blogInfo, blogDetailResponse);
        return blogDetailResponse;
    }
}
